package com.gateway.video.rtsp.handler;

import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;

import java.nio.charset.Charset;

@Slf4j
@RequiredArgsConstructor
public class RtspClientHandler extends ChannelDuplexHandler {
    private final ChannelHandlerContext serverChannel;

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if(msg instanceof HttpResponse) {
            var response = (FullHttpResponse) msg;

            serverChannel.writeAndFlush(response).addListener(future -> {
               if(future.isSuccess()) {
                   if(log.isDebugEnabled()) {
                       log.debug("Video client send response {}", response);
                   }
                   serverChannel.read();
               }else {
                   if(log.isErrorEnabled()) {
                       log.error("Video client send response fail", future.cause());
                   }
               }
            });
        }
    }

    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
        super.write(ctx, msg, promise);
    }
}
